
import { join } from 'node:path';
import { release } from 'node:os';
import { app, BrowserWindow } from 'electron';
import { createPySheme } from "../python/index.ts";

if (release().startsWith('6.1')) app.disableHardwareAcceleration();

if (process.platform === 'win32') app.setAppUserModelId(app.getName());

export const ROOT_PATH = {
  storePath: join(app.getPath('documents'), '/identification'), // 构建存储目录
  dist: join(__dirname, '../..')
};

process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';

// let win: BrowserWindow | null;
const url = process.env.VITE_DEV_SERVER_URL as string;
const indexHtml = join(ROOT_PATH.dist, 'index.html');
function createWindow() {
  const win = new BrowserWindow({
    title: 'HY',
    maximizable: true,
    frame: false, // 无边框窗口
    transparent: false, // 使窗口透明
    alwaysOnTop: false, // 设置窗口始终在最顶层
    fullscreen: true,
    width: 1560,
    height: 1000,
    minHeight: 650,
    minWidth: 1000,
    resizable: true,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      webSecurity: false, // 禁用同源策略
      allowRunningInsecureContent: true, // 允许加载不安全内容
      experimentalFeatures: true, // 启用实验特性
      preload: join(__dirname, '../preload/index.js'),
    }
  });

  win.maximize(); // 默认最大化窗口

  if (app.isPackaged) {
    win.loadFile(indexHtml);
    // win.webContents.openDevTools();

  } else {
    win.loadURL('http://localhost:9527/')
    // win.webContents.openDevTools(); 
  }
  // win.webContents.openDevTools();

  return win;
}

app.whenReady().then(async () => {
  createPySheme()
  createWindow();

  // createAllPythonScript()
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});
